﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Mbs;
using Mbs.Modularity;

namespace ModuleManager.Module
{
    public partial class ModuleManagerView : GridControl<ModuleInfo>,IModuleManagerView
    {
        public ModuleManagerView()
        {
            InitializeComponent();
        }

        public override IEnumerator<IDisposable> Attach()
        {
            yield return CommandNames.ModuleSelectedChanged.Attach(BindingSource, "CurrentChanged");
        }

        public override void Init()
        {
            GridView.ContextMenuStrip = this.BuildItem<ContextMenuStrip>(AddInItems.ContextMenu);
            BindingSource.AllowNew = false;
            //GridView.CellFormatting += new DataGridViewCellFormattingEventHandler(GridView_CellFormatting);
        }

        void GridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == 3)
                    e.Value = EnumHelper.Parse<ModuleState>(e.Value.ToString()).GetDescription();
            }
        }

        public override void InitializeResource()
        {
            AddColumns(new ColumnDescriptor[]
            {
                new ColumnDescriptor{ /*Title = "${res:Global.Id}",*/ Property="Id", Type = typeof(int)}
                , new ColumnDescriptor{/* Title = "${res:Global.Name}",*/ Property="Name", Type = typeof(string)}
                , new ColumnDescriptor{ /*Title = "${res:Global.State}",*/ Property="State", Type = typeof(string)}
                , new ColumnDescriptor{/* Title = "${res:Global.StartMode}",*/ Property="StartMode", Type = typeof(string)}
                , new ColumnDescriptor{ /*Title = "${res:Global.Description}",*/ Property="Description", Type = typeof(string)}
            });
        }

       

        public void Bind(IEnumerable<ModuleInfo> modules)
        {
            var items = modules.ToArray();
            base.Populate(items);
        }

        public int CurrentId
        {
            get
            {
                return base.CurrentItem.ModuleId;
            }
            set
            {
                var item = Find((m) => m.ModuleId == value);
                if (item != null)
                    CurrentItem = item;
            }
        }

        public string Open()
        {
            using (var dlg = new OpenFileDialog())
            {
                dlg.Filter = "dll file(*.dll)|*.dll";
                dlg.CheckFileExists = true;
                dlg.CheckPathExists = true;
                dlg.InitialDirectory = MbsEnvironment.AddInPath;

                switch (dlg.ShowDialog())
                {
                    case DialogResult.OK:
                    case DialogResult.Yes:
                        return dlg.FileName;
                    default: return null;
                }
            }
        }
    }
}
